package com.zw.pdm.module.designtask.service;

import com.zw.pdm.constant.Constant;
import com.zw.pdm.entity.designtask.CosDesign;
import com.zw.pdm.entity.designtask.CosWeight;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.designtask.mapper.WeightHoursMapper;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 设计工时、设备重量 服务层
 * @Author: China.wzy
 */
@Service
@SuppressWarnings("all")
public class WeightHoursService {

    @Autowired
    private WeightHoursMapper weighthoursMapper;

    /**
     * 查询设备
     * @param code
     * @param type
     * @return
     */
    public List<DsnProStruct> findPartByProjectCode(String code, Integer type) {
        return weighthoursMapper.findPartByProjectCode( code,  type);
    }

    /***
     * 加载人工工时
     * @param cosDesign
     * @return
     */
    public List<CosDesign> designHours(CosDesign cosDesign) {
        cosDesign.setCreateCode(ShiroUtils.getUser().getAccount());
        return weighthoursMapper.designHours(cosDesign);
    }

    /***
     * 加载设备重量
     * @param cosWeight
     * @return
     */
    public List<CosWeight> designWeight(CosWeight cosWeight) {
        cosWeight.setCreateCode(ShiroUtils.getUser().getAccount());
        return weighthoursMapper.designWeight(cosWeight);
    }

    /**
     * 保存/修改设计工时
     * @param data
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int hoursSaveData(CosDesign data) {
        User user = ShiroUtils.getUser();
            if (data.getId() == null){
                data.setDesInputUser(user.getName());
                data.setDeptId(user.getDeptId());
                data.setDepts(user.getBranchName());
                data.setCreateCode(user.getAccount());
                data.setDesEditDate(new Date());
                data.setFkId(data.getFkId());
                data.setFkCode(data.getFkCode());
                data.setCostKind("设计工时、重量");
                data.setFkInputId(20);
                data.setDesDeleteFlag(0);
                data.setDesReceiveFlag(0);
                data.setStatus(1);
                data.setDesignType(data.getSpecial());
                if (data.getSpecial() == 60 ) {
                    data.setDesignType(1) ;
                }else if (data.getSpecial() == 62){
                    data.setDesignType(2);
                }else if (data.getSpecial() == 61){
                    data.setDesignType(3);
                }
                weighthoursMapper.insertDesign(data);
            }else {
                weighthoursMapper.updateDesign(data);
            }
            return 1;
    }

    /**
     * 删除设计工时
     * @param desId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int delHouseOne(Integer id) {
        return weighthoursMapper.delHoursUp(id, Constant.STATUS_NORMAL);
    }

    /**
     *保存/修改设备重量
     * @param entry
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int weightSaveData(CosWeight entry) {
        User user = ShiroUtils.getUser();
        if (entry.getId() == null){
            entry.setWeiInputUser(user.getName());
            entry.setDeptId(user.getDeptId());
            entry.setDepts(user.getBranchName());
            entry.setCreateCode(user.getAccount());
            entry.setWeiEditDate(new Date());
            entry.setFkId(entry.getFkId());
            entry.setFkCode(entry.getFkCode());
            entry.setCostKind("设计工时、重量");
            entry.setFkInputId(20);
            entry.setWeiDeleteFlag(0);
            entry.setWeiReceiveFlag(0);
            entry.setStatus(1);
            entry.setDesignType(entry.getSpecial());
            if (entry.getSpecial() == 60 ) {
                entry.setDesignType(1) ;
            }else if (entry.getSpecial() == 62){
                entry.setDesignType(2);
            }else if (entry.getSpecial() == 61){
                entry.setDesignType(3);
            }
            weighthoursMapper.insertEquipment(entry);
        }else {
            weighthoursMapper.updateEquipment(entry);
        }
        return 2;
    }

    /**
     * 删除设备重量
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int delWeightOne(Integer id) {
        return weighthoursMapper.delWeightUp(id,Constant.STATUS_NORMAL);
    }

    /**
     * 提交更改提交状态
     * @param cosDesign
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int submitData(CosDesign cosDesign) {
        cosDesign.setCreateCode(ShiroUtils.getUser().getAccount());
        int changeState = weighthoursMapper.alterDesign(cosDesign);
        changeState += weighthoursMapper.alterWeight(cosDesign);
        return changeState;
    }
}
